package com.demo.utils;

import com.demo.core.ActionContext;
import com.demo.core.Constants;
import com.demo.core.SysConfig;
import com.demo.po.ServerPo;
import com.google.common.primitives.Chars;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.Session;
import org.apache.commons.lang3.CharSet;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class ShellUtils {

    public static final Integer DEFAULT_TIMEOUT = 30000;

    public static boolean exists(Session session, String filePath) {
        boolean exist = true;
        ChannelSftp sftp = null;
        try {
            sftp = (ChannelSftp) session.openChannel("sftp");
            sftp.connect(DEFAULT_TIMEOUT);
            sftp.ls(filePath);
        } catch (Exception ex) {
            ex.printStackTrace();
            exist = false;
        } finally {
            if (sftp != null)
                try {
                    sftp.disconnect();
                } catch (Exception e) {
                }
        }
        return exist;
    }

    public static boolean execFtp(Session session, String bash, String remote) {
        boolean result = true;

        ChannelSftp sftp = null;
        try {
            sftp = (ChannelSftp) session.openChannel("sftp");
            sftp.connect(DEFAULT_TIMEOUT);
            InputStream is = new ByteArrayInputStream(bash.getBytes());
            sftp.setInputStream(is);
//            sftp.setFilenameEncoding(Constants.ENCODE_UTF8);
            sftp.put(is, remote);
            is.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            result = false;
        } finally {
            if (sftp != null)
                try {
                    sftp.disconnect();
                } catch (Exception e) {
                }
        }
        return result;
    }

    public static List<String> execCmd(Session session, String command) {
        ArrayList<String> list = new ArrayList<>();

        ChannelExec channel = null;
        try {
            channel = (ChannelExec) session.openChannel("exec");
            channel.setCommand(command);
            channel.setInputStream(null);
            channel.setErrStream(System.err);
            channel.connect(DEFAULT_TIMEOUT);
            InputStream is = channel.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is, Charset.forName(Constants.ENCODE_UTF8)));
            String line = null;
            while ((line = br.readLine()) != null) {
                list.add(line);
            }
            br.close();
            is.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            list = null;
        } finally {
            if (channel != null)
                try {
                    channel.disconnect();
                } catch (Exception e) {
                }
        }
        return list;
    }


    public static void main(String[] args) {
        ServerPo server = new ServerPo();
        server.setHost("115.29.102.182");
        server.setSshport(22);
        server.setSshname("root");
        server.setSshpwd("520Beany");
//        Session session = ActionContext.getSession(server);
//        StringBuffer sb = new StringBuffer();
//        sb.append("");
//        boolean result = sftp(session, "#!bash", "/root/test.sh");
//        execCmd(session, "cd /root/bdev/ && sh a.sh start");
//        session.disconnect();
    }
}
